// 引入 App 原生语言插件
// #ifdef APP-PLUS
const jpushModule = uni.requireNativePlugin('JG-JPush')
// #endif

// 极光推送初始化
export const JPushInit = async () => {
  // 初始化推送服务
  if (uni.getSystemInfoSync().platform === 'ios') {
    console.log('platform ===> ios')
    // 请求定位权限
    const locationServicesEnabled = jpushModule.locationServicesEnabled()
    const locationAuthorizationStatus = jpushModule.getLocationAuthorizationStatus()
    console.log('locationAuthorizationStatus', locationAuthorizationStatus)
    if (locationServicesEnabled === true && locationAuthorizationStatus < 3) {
      jpushModule.requestLocationAuthorization((result) => {
        console.log('定位权限', result.status)
      })
    }

    jpushModule.requestNotificationAuthorization((result) => {
      const status = result.status
      if (status < 2) {
        uni.showToast({
          icon: 'none',
          title: '您还没有打开通知权限',
          duration: 3000,
        })
      }
    })

    jpushModule.addGeofenceListener((result) => {
      const code = result.code
      const type = result.type
      const geofenceId = result.geofenceId
      const userInfo = result.userInfo
      uni.showToast({
        icon: 'none',
        title: '触发地理围栏',
        duration: 3000,
      })
    })

    // 监听deviToken的状态
    jpushModule.addDeviceTokenListener((result) => {
      const code = result.code
      if (code === 0) {
        const deviceToken = result.deviceToken
        uni.showToast({
          icon: 'none',
          title: deviceToken,
          duration: 3000,
        })
        console.log('deviceToken:', deviceToken)
      } else {
        const error = result.msg
      }
    })
  } else {
    console.log('platform === Android')
  }

  // Android
  jpushModule.initJPushService()
  jpushModule.setLoggerEnable(true)

  jpushModule.addConnectEventListener((result) => {
    const connectEnable = result.connectEnable
    uni.$emit('connectStatusChange', connectEnable)
  })

  jpushModule.addNotificationListener((result) => {
    const notificationEventType = result.notificationEventType
    const messageID = result.messageID
    const title = result.title
    const content = result.content
    const extras = result.extras
    console.log('addNotificationListener:', result)
    // uni.showToast({
    //   icon: 'none',
    //   title: JSON.stringify(result),
    //   duration: 3000,
    // })
    // if (notificationEventType === 'notificationOpened') {
    //   uni.navigateTo({
    //     url: '/pages/index/index',
    //   })
    // }
  })

  jpushModule.addCustomMessageListener((result) => {
    const type = result.type
    const messageType = result.messageType
    const content = result.content
    uni.showToast({
      icon: 'none',
      title: JSON.stringify(result),
      duration: 3000,
    })
  })
  jpushModule.addInMessageListener((result) => {
    // uni.showToast({
    //   icon: 'none',
    //   title: JSON.stringify(result),
    //   duration: 3000,
    // })
  })
  jpushModule.addLocalNotificationListener((result) => {
    const messageID = result.messageID
    const title = result.title
    const content = result.content
    const extras = result.extras
    // uni.showToast({
    //   icon: 'none',
    //   title: JSON.stringify(result),
    //   duration: 3000,
    // })
  })
}

export const JPushGetRegistrationID = async () => {
  return new Promise((resolve, reject) => {
    jpushModule.getRegistrationID((result) => {
      console.log('getRegistrationID======>', result)
      if (result) {
        resolve(result.registerID) // 成功时返回结果
      } else {
        reject(new Error('Failed to get registration ID')) // 失败时返回错误
      }
    })
  })
}

export const JPushSetAlias = async (alias) => {
  // 设置别名
  jpushModule.setAlias({
    alias,
    sequence: 1, // sequence 是一个可选参数，用于标识请求的顺序
  })
}
